package com.lmy.dp01.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
@Slf4j
public class HttpAspect {
    @Pointcut("execution(* com.lmy.dp01.api.StudentApiController.*(..))")
    public void pointcut() {}

    @Before("pointcut()")
    public void before() {
        log.info("准备开始执行aop······");
    }

    @After("pointcut()")
    public void after() {
        log.info("aop执行完毕······");
    }

    @AfterReturning(returning = "object",pointcut = "pointcut()")
    public void afterReturning(Object object) {
        log.info("response={}", object);
    }


    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 观察对象，计时
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // 真正执行的方法
        Object result = joinPoint.proceed(joinPoint.getArgs());
        stopWatch.stop();
        long time = stopWatch.getTotalTimeMillis();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String methodName = methodSignature.getDeclaringTypeName() + "." + methodSignature.getName();
        log.info("···········执行"+methodName+"方法，用时："+time+"ms···········");
        return result;
    }
}
